package com.java.imauoj.controler;

import com.java.imauoj.moudel.ResultVo;
import com.java.imauoj.moudel.Role;
import com.java.imauoj.moudel.User;
import com.java.imauoj.sercive.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("api/v1")
public class RoleControler {

    @Autowired
    private RoleService service;

    @GetMapping("getRoleList")
    public ResultVo<List<Role>> getRoleList(HttpSession session){
        ResultVo re = new ResultVo();
        User userLogin = (User)session.getAttribute("login");
        if(userLogin!=null){
            Role role = (Role)session.getAttribute("role");
            if(role.getIs_get_role() == 0){
                re.setStatusCode(0);
                re.setMessage("没有权限");
            }else{
                re.setStatusCode(1);
                re.setMessage("成功");
                re.setData(service.getRoleList());
            }
        }
        return re;
    }

    @PostMapping("roleOperate")
    public ResultVo roleOperate(@RequestParam(value = "active") int active, Role role, HttpSession session){

        ResultVo re = new ResultVo();
        User userLogin = (User)session.getAttribute("login");
        if(userLogin!=null){
            Role sRole = (Role)session.getAttribute("role");
            if(sRole.getIs_ope_role() == 0){
                re.setStatusCode(0);
                re.setMessage("没有权限");
            }else{
                re.setStatusCode(1);
                if(active == 0){
                    Role role1 = service.updateRole(role);
                    re.setData(role1);
                    re.setMessage("修改成功");
                }else if(active == 1){
                    Role role1 = service.addRole(role);
                    re.setData(role1);
                    re.setMessage("添加成功");
                }else if(active == -1){
                    Role role1 = service.deleteRole(role.getRole_id());
                    re.setData(role1);
                    re.setMessage("删除成功");
                }
            }
        }
        return re;

    }
}
